using System.Collections;
using UnityEngine;

public class OptimizeSound : MonoBehaviour
{
	private AudioSource cur_audio;

	private float curDist;

	private void Awake()
	{
		cur_audio = GetComponent<AudioSource>();
		StartCoroutine(CheckPlaySound());
	}

	private IEnumerator CheckPlaySound()
	{
		if (!(cur_audio != null))
		{
			yield break;
		}
		while (true)
		{
			if (GameController.instance.myPlayer != null)
			{
				curDist = Vector3.Distance(base.transform.position, GameController.instance.myPlayer.transform.position);
				if (curDist < cur_audio.maxDistance)
				{
					if (!cur_audio.enabled)
					{
						cur_audio.enabled = true;
						cur_audio.Play();
					}
				}
				else if (cur_audio.enabled)
				{
					cur_audio.Stop();
					cur_audio.enabled = false;
				}
			}
			yield return new WaitForSeconds(0.3f);
		}
	}
}
